package com.glsc.ngateway.common.base.repo.mysql.gateway.other;

import com.glsc.ngateway.common.base.domain.mysql.gateway.other.TradeSeatHistory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface TradeSeatHistoryRepository extends JpaRepository<TradeSeatHistory, Integer>, JpaSpecificationExecutor<TradeSeatHistory> {
    TradeSeatHistory findByTradeSeatId(Integer tradeSeatId);
    TradeSeatHistory findByTradeSeatCodeAndTradeSeatMarket(String tradeSeatCode, String tradeSeatMarket);

    //分页查询交易单元汇总
    @Query(value = "select * from t_am_trade_seat_history tsh inner join (select trade_seat_code,max(deal_date) deal_date from(select trade_seat_code,(case when state = 1 then begin_date else end_date end) deal_date from t_am_trade_seat_history) s group by trade_seat_code) a on tsh.trade_seat_code = a.trade_seat_code and tsh.begin_date = a.deal_date or tsh.end_date = a.deal_date where (:tradeSeatCode is null or tsh.trade_seat_code like concat('%',:tradeSeatCode,'%')) and (:tradeSeatMarket is null or tsh.trade_seat_market = :tradeSeatMarket)", nativeQuery = true)
    Page<TradeSeatHistory> findTradeSeatHistorySumPage(String tradeSeatCode, String tradeSeatMarket,Pageable pageable);
}
